package cn.bupt.sse309.ydl.activity;

import cn.bupt.sse309.ydl.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

public class PersonalImfoActivity extends Activity implements OnClickListener {
  /* 组件 */
  private RelativeLayout switchAvatarRl;
  private ImageView faceImageIv;
  private String[] imagitems = new String[] { "选择本地图片", "拍照" };
  /* 头像名称 */
  private static final String IMAGE_FILE_NAME = "faceImage.jpg";

  /* 请求码 */
  private static final int IMAGE_REQUEST_CODE = 0;
  private static final int CAMERA_REQUEST_CODE = 1;
  private static final int RESULT_REQUEST_CODE = 2;


  private Button backBtn, finishBtn;

  private TextView titleTv, amendPasswordTv;
  private RelativeLayout professionalRl, areaRl, sexRl;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    this.setContentView(R.layout.activity_personal_imfo);
    this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.base_title_with_back);
    this.initView();
  }

  /**
   *
   */
  public void initView() {
    // TODO Auto-generated method stub
    this.titleTv = (TextView) this.findViewById(R.id.baseTitleId);
    this.backBtn = (Button) this.findViewById(R.id.baseBack);
    this.titleTv.setText(R.string.title_personal_imfo);
    this.finishBtn = (Button) this.findViewById(R.id.btn_finish);
    this.amendPasswordTv = (TextView) this.findViewById(R.id.tv_amend_password);
    this.professionalRl = (RelativeLayout) this.findViewById(R.id.rl_professional);
    this.areaRl = (RelativeLayout) this.findViewById(R.id.rl_area);
    this.sexRl = (RelativeLayout) this.findViewById(R.id.rl_sex);
    this.professionalRl.setOnClickListener(this);
    this.areaRl.setOnClickListener(this);
    this.sexRl.setOnClickListener(this);
    this.finishBtn.setOnClickListener(this);
    this.backBtn.setOnClickListener(this);

    this.switchAvatarRl = (RelativeLayout) this.findViewById(R.id.rl_switch_face);
    this.faceImageIv = (ImageView) this.findViewById(R.id.iv_face);
    // 设置事件监听
    this.switchAvatarRl.setOnClickListener(this);

  }

  /*
   * (non-Javadoc)
   *
   * @see android.view.View.OnClickListener#onClick(android.view.View)
   */
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()) {
    case R.id.rl_switch_face:
      this.showDialog();

      break;
    case R.id.baseBack:
      PersonalImfoActivity.this.finish();
      break;
    case R.id.btn_finish:
      Intent intent = new Intent(PersonalImfoActivity.this, MainActivity.class);
      this.startActivity(intent);

      break;
    case R.id.rl_sex:
      // Intent intent2 =new Intent(PersonalImfoActivity.this,ResetPasswordActivity.class);
      // this.startActivity(intent2);
      Toast.makeText(this, "性别", Toast.LENGTH_SHORT).show();

      break;
    case R.id.rl_area:
      // Intent intent2 =new Intent(PersonalImfoActivity.this,ResetPasswordActivity.class);
      // this.startActivity(intent2);
      Toast.makeText(this, "区域", Toast.LENGTH_SHORT).show();

      break;
    case R.id.rl_professional:
      // Intent intent2 =new Intent(PersonalImfoActivity.this,ResetPasswordActivity.class);
      // this.startActivity(intent2);
      Toast.makeText(this, "行业", Toast.LENGTH_SHORT).show();

      break;

    default:
      break;
    }

  }

  /**
   * 显示选择对话框
   */
  private void showDialog() {

    new AlertDialog.Builder(this)
        .setTitle("设置头像")
        .setItems(this.imagitems, new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {
            switch (which) {
            case 0:
              Intent intentFromGallery = new Intent();
              intentFromGallery.setType("image/*"); // 设置文件类型
              intentFromGallery
                  .setAction(Intent.ACTION_GET_CONTENT);
              PersonalImfoActivity.this.startActivityForResult(intentFromGallery,
                  IMAGE_REQUEST_CODE);
              break;
            case 1:

              Intent intentFromCapture = new Intent(
                  MediaStore.ACTION_IMAGE_CAPTURE);
              // 判断存储卡是否可以用，可用进行存储
              if ( hasSdcard()) {

                intentFromCapture.putExtra(
                    MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory(),
                        IMAGE_FILE_NAME)));
              }

              PersonalImfoActivity.this.startActivityForResult(intentFromCapture,
                  CAMERA_REQUEST_CODE);
              break;
            }
          }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        }).show();

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //结果码不等于取消时候
    if (resultCode != RESULT_CANCELED) {

      switch (requestCode) {
      case IMAGE_REQUEST_CODE:
        this.startPhotoZoom(data.getData());
        break;
      case CAMERA_REQUEST_CODE:
        if (  hasSdcard()) {
          File tempFile = new File(
              Environment.getExternalStorageDirectory()
                  + IMAGE_FILE_NAME);
          this.startPhotoZoom(Uri.fromFile(tempFile));
        } else {
          Toast.makeText(PersonalImfoActivity.this, "未找到存储卡，无法存储照片！",
              Toast.LENGTH_LONG).show();
        }

        break;
      case RESULT_REQUEST_CODE:
        if (data != null) {
          this.getImageToView(data);
        }
        break;
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  /**
   * 裁剪图片方法实现
   *
   * @param uri
   */
  public void startPhotoZoom(Uri uri) {

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    // 设置裁剪
    intent.putExtra("crop", "true");
    // aspectX aspectY 是宽高的比例
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    // outputX outputY 是裁剪图片宽高
    intent.putExtra("outputX", 320);
    intent.putExtra("outputY", 320);
    intent.putExtra("return-data", true);
    this.startActivityForResult(intent, 2);
  }

  /**
   * 保存裁剪之后的图片数据
   *
   * @param picdata
   */
  private void getImageToView(Intent data) {
    Bundle extras = data.getExtras();
    if (extras != null) {
      Bitmap photo = extras.getParcelable("data");
      Drawable drawable = new BitmapDrawable(photo);
      this.faceImageIv.setImageDrawable(drawable);
    }
  }
  /**
   * 检查是否存在SDCard
   * @return
   */
  public static boolean hasSdcard(){
    String state = Environment.getExternalStorageState();
    if(state.equals(Environment.MEDIA_MOUNTED)){
      return true;
    }else{
      return false;
    }
  }




}
